package com.future.controller;

import javax.validation.constraints.NotNull;

import com.future.common.annotation.ControllerMapping;
import com.future.common.base.BaseController;
import com.future.dao.LearnDao;
import com.future.model.Learn;
import com.future.requestbean.LearnAdd;
import com.future.requestbean.LearnUpdate;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.tx.Tx;

@ControllerMapping("/learn")
public class LearnController extends BaseController {
	@Inject
	private LearnDao LearnDao;

	@Before(Tx.class)
	public void addLearn() {
		LearnAdd learnAdd = getJsonBody(LearnAdd.class);
		Learn learn = beanToModel(learnAdd, Learn.class);
		learn.setHeadimg(getFirstImg(learnAdd.getContent()));
		this.LearnDao.save(learn);
		renderSuccess();
	}

	@Before(Tx.class)
	public void deleteLearn(@NotNull(message = "选择删除的对象") Long learnId) {
		this.LearnDao.deleteById(learnId);
		renderSuccess();
	}

	public void queryLearn() {
		renderResult("learns", this.LearnDao.findAll());
	}

	@Before(Tx.class)
	public void learnUpdata() {
		Learn learn = getJsonBodyToModel(LearnUpdate.class, Learn.class);
		this.LearnDao.update(learn);
		renderSuccess();
	}
}
